001    /*
002     * Copyright (C) 2002, Francois Beausoleil
003     *
004     * This program is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU General Public License as published by the
006     * Free Software Foundation; either version 2 of the License, or any later
007     * version.
008     *
009     * This program is distributed in the hope that it will be useful, but
010     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
011     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
012     * for more details.
013     *
014     * You should have received a copy of the GNU General Public License along
015     * with this program; if not, write to the Free Software Foundation, Inc.,
016     * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017     *
018     * You may contact the author of this software at: fbos@users.sourceforge.net
019     */
020    
021    package jgb.examples;
022    
023    import jgb.builder.DefaultBuilder;
024    import jgb.builder.Builder;
025    
026    import javax.swing.*;
027    import java.awt.event.ActionEvent;
028    import java.awt.event.ActionListener;
029    import java.io.ByteArrayInputStream;
030    import java.io.File;
031    
032    /**
033     * @since 0.1a
034     * @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a>
035     */
036    public class InLineWindowTest {
037        public static final String DTD_PATHNAME = "jgb/dtd/jgb.dtd";
038        public static final File DTD_FILE = new File(DTD_PATHNAME);
039        public static final String[] xmlWindow = {
040            "<windows>",
041            "   <window id=\"main\" type=\"jdialog\">",
042            "       <controls>",
043            "           <constraints><constant name=\"java.awt.BorderLayout.CENTER\" /></constraints>",
044            "           <layout class=\"GridLayout\">",
045            "               <property name=\"columns\"><value type=\"int\" data=\"2\" /></property>",
046            "               <control class=\"JLabel\">",
047            "                   <property name=\"text\"><value type=\"string\" data=\"File to read:\" /></property>",
048            "                   <property name=\"displayedMnemonic\">",
049            "                       <constant name=\"java.awt.event.KeyEvent.VK_F\" />",
050            "                   </property>",
051            "               </control>",
052            "               <control id=\"fileNameField\" class=\"JTextField\" />",
053            "           </layout>",
054            "           <constraints><constant name=\"java.awt.BorderLayout.SOUTH\" /></constraints>",
055            "           <layout class=\"GridLayout\">",
056            "               <property name=\"columns\"><value type=\"int\" data=\"2\" /></property>",
057            "               <control id=\"generateButton\" class=\"JButton\">",
058            "                   <property name=\"text\"><value type=\"string\" data=\"Generate\" /></property>",
059            "                   <property name=\"mnemonic\">",
060            "                      <constant name=\"java.awt.event.KeyEvent.VK_G\" />",
061            "                   </property>",
062            "              </control>",
063            "               <control id=\"exitButton\" class=\"JButton\">",
064            "                   <property name=\"text\"><value type=\"string\" data=\"Exit\" /></property>",
065            "                   <property name=\"mnemonic\">",
066            "                       <constant name=\"java.awt.event.KeyEvent.VK_X\" />",
067            "                  </property>",
068            "               </control>",
069            "           </layout>",
070            "       </controls>",
071            "   </window>",
072            "</windows>",
073        };
074    
075        public static void main(String[] args) throws Exception {
076            StringBuffer xmlSource = new StringBuffer();
077            xmlSource.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
078            xmlSource.append('\n');
079    
080            xmlSource.append("<!DOCTYPE windows SYSTEM \"");
081            xmlSource.append(DTD_FILE.toURL().toString());
082            xmlSource.append("\">");
083            xmlSource.append('\n');
084    
085            for (int i = 0; i < xmlWindow.length; i++) {
086                xmlSource.append(xmlWindow[i])
087                        .append('\n');
088    
089            }
090    
091            System.out.println(xmlSource);
092    
093            Builder builder = new DefaultBuilder();
094            builder.build(new ByteArrayInputStream(xmlSource.toString().getBytes()));
095            JDialog dialog = (JDialog)builder.getObject("main");
096            JTextField fileNameField = (JTextField)builder.getObject("fileNameField");
097            JButton generateButton = (JButton)builder.getObject("generateButton");
098            JButton exitButton = (JButton)builder.getObject("exitButton");
099    
100            exitButton.addActionListener(new ActionListener() {
101                public void actionPerformed(ActionEvent e) {
102                    System.exit(0);
103                }
104            });
105    
106            dialog.pack();
107            dialog.setVisible(true);
108        }
109    }